package com.sita.tboard.ui.activity;

import android.app.LocalActivityManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.sita.bike.R;
import com.sita.bike.support.GlobalContext;
import com.sita.bike.utils.BluetoothUtils;
import com.sita.tboard.ActivityBase;

import java.util.ArrayList;
import java.util.List;

import app.akexorcist.bluetotohspp.library.BluetoothSPP;

//import android.support.v4.app.Fragment;

/**
 * Created by nilesh on 8/27/2015.
 */
public class SwipActivity extends ActivityBase {
    Context context = null;
    LocalActivityManager manager = null;
    private ViewPager pager = null;
    private DashboardActivity dashboardactivity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tboard_activity_swipe);
        context = SwipActivity.this;
        manager = new LocalActivityManager(this, true);
        manager.dispatchCreate(savedInstanceState);

        initPagerViewer();

//        viewPager=(ViewPager)findViewById(R.id.pager);
//        PageAdapter pageAdapter=new PageAdapter(getSupportFragmentManager());
//        viewPager.setAdapter(pageAdapter);


    }

    /**
     * 初始化PageViewer
     */
    private void initPagerViewer() {
        pager = (ViewPager) findViewById(R.id.pager);
        final ArrayList<View> list = new ArrayList<View>();
        Intent intent = new Intent(context, HomeActivity.class);
        list.add(getView("A", intent));
        Intent intent2 = new Intent(context, DashboardActivity.class);
        list.add(getView("B", intent2));
//        Intent intent3 = new Intent(context, TboardTeamActivity.class);
//        list.add(getView("C", intent3));

        pager.setAdapter(new MyPagerAdapter(list));
        pager.setCurrentItem(0);
//        pager.setOnPageChangeListener(new MyOnPageChangeListener());

        //滑动监听器
        pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        break;
                    case 1:
                        BluetoothUtils.autoConnect();
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    /**
     * 通过activity获取视图
     *
     * @param id
     * @param intent
     * @return
     */
    private View getView(String id, Intent intent) {
        return manager.startActivity(id, intent).getDecorView();
    }

    /**
     * Pager适配器
     */
    public class MyPagerAdapter extends PagerAdapter {
        List<View> list = new ArrayList<View>();

        public MyPagerAdapter(ArrayList<View> list) {
            this.list = list;
        }

        @Override
        public void destroyItem(ViewGroup container, int position,
                                Object object) {
            ViewPager pViewPager = ((ViewPager) container);
            pViewPager.removeView(list.get(position));
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object instantiateItem(View arg0, int arg1) {
            ViewPager pViewPager = ((ViewPager) arg0);
            pViewPager.addView(list.get(arg1));
            return list.get(arg1);
        }

        @Override
        public void restoreState(Parcelable arg0, ClassLoader arg1) {

        }

        @Override
        public Parcelable saveState() {
            return null;
        }

        @Override
        public void startUpdate(View arg0) {
        }
    }

//    public void onActivityResult(int requestCode, int resultCode, Intent data) {
//        DashboardActivity dashboardactivity = (DashboardActivity) manager.getActivity("B");
//        dashboardactivity.handleActivityResult(requestCode, resultCode, data);
//    }
}
